/*
** gere_key.c for  in /u/all/caille_c/cu/public/colle
** 
** Made by carole cailleux
** Login   <caille_c@epitech.net>
** 
** Started on  Mon Apr 26 22:12:45 2010 carole cailleux
** Last update Mon Apr 26 22:57:10 2010 carole cailleux
*/

#include <stdlib.h>
#include <ncurses.h>
#include <termios.h>
#include <termcap.h>
#include <unistd.h>
#include "starwars.h"

void		move_cursor(int x, int y)
{
  char		*term;
  char		bp[1024];
  char		t[4096];
  char		*area;
  char		*cmstr;

  if ((term = getenv("TERM")) == NULL)
    {
      my_puterror("Cannot get env.\n");
      exit (1);
    }
  tgetent(bp, term);
  area = t;
  cmstr = tgetstr("cm", &area);
  tputs(tgoto(cmstr, x, y), 1, my_outc);
}

void		move_left(int *x, int y)
{
  *x -= 1;
  if (*x > 0)
    {
      move_cursor(*x, y - 5);
      main_starship();
    }
}

void		move_right(int *x, int y)
{
  *x += 1;
  if (*x < 71)
    {
      move_cursor(*x, y - 5);
      main_starship();
    }
}

void		gere_key(t_params *params)
{
  char		buf[6];

  while (1)
    {
      buf[1] = 0;
      buf[2] = 0;
      read(0, buf, 3);
      if (buf[0] == 27 && buf[1] != 91)
	exit(1);
      else if (buf[0] == 27 && buf[2] == 67)
	move_right(&(params->x), params->y);
      else if (buf[0] == 27 && buf[2] == 68)
	move_left(&(params->x), params->y);
    }
}
